﻿@inject IHttpService _httpService


@inject NavigationManager NavigationManager


<SectionTitleCard Title="激活码列表" Text="奖品的激活码，也可用占位符替代" />
<div>
    <CnGalWebSite.Components.Buttons.MasaButton Text="添加" OnClick="AddPrize" Icon="@IconType.Create.ToIconString()" Color="info" />
    <CnGalWebSite.Components.Buttons.MasaButton Text="导入" OnClick="BatchItems" Icon="@IconType.Import.ToIconString()" Class="ms-2" Color="success" />
</div>
@if (Prizes.Any())
{
    <MSimpleTable Class="mb-4">
        <thead>
            <tr>
                <th class="text-left">
                   内容
                </th>
                <th class="text-left">
                    操作
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Prizes)
            {
                <tr @key="Prizes.IndexOf(item)">
                    <td style="min-width: 50px; ">@item.Context</td>
                    <td>
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="编辑" OnClick="@(()=>EditPrize(item))" Icon="@IconType.Edit.ToIconString()" Color="success" />
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="删除" OnClick="@(()=>DeletePrize(item))" Icon="@IconType.Delete.ToIconString()" Color="error" />
                    </td>
                </tr>
            }
        </tbody>
    </MSimpleTable>
}

<MDialog @bind-Value="_editDialog"
         MaxWidth="800">
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.EditCard Prizes="Prizes" OnEdited="OnEdited" @ref="editCard" Model="_editedItem" IsAdd="_isAdd"/>
</MDialog>
<MDialog @bind-Value="_batchDialog"
         MaxWidth="800">
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.BatchCard OnBatched="OnBatched" @ref="batchCard" Title="激活码" />
</MDialog>

@code {
    [Parameter]
    public List<EditLotteryPrizeModel> Prizes { get; set; }

    CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.EditCard editCard;
    private bool _editDialog;
    private long _editedItemId;
    private bool _isAdd;
    private EditLotteryPrizeModel _editedItem;

    CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.BatchCard batchCard;
    private bool _batchDialog;

    public async Task DeletePrize(EditLotteryPrizeModel item)
    {
        Prizes.Remove(item);
        StateHasChanged();
    }
    public void EditPrize(EditLotteryPrizeModel item)
    {
        _editDialog=true;
        _editedItem = item;
        _isAdd = false;
        StateHasChanged();
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
    }


    public void AddPrize()
    {
        _editDialog = true;
        _editedItem = new EditLotteryPrizeModel();
        _isAdd = true;

        StateHasChanged();
    }

    public void BatchItems()
    {
        _batchDialog = true;
        StateHasChanged();
    }


    public void OnBatched(List<string> model)
    {
        //检查是否符合要求
        foreach (var item in model)
        {
            Prizes.Add(new EditLotteryPrizeModel
                {
                    Context = item
        });
            
        }
        _batchDialog = false;
        StateHasChanged();
    }
}
